//
//  YPRecommendCell.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/1/6.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPRecommendCell.h"
#import "YPBorrowTypeView.h"

@interface YPRecommendCell (){
    
    UILabel *_titleLabel;
    YPBorrowTypeView *_borrowTypeView;
    UILabel *_aprLabel; //年利率
    UILabel *_timeLimit;
    UILabel *_timeLimitLabel; //期限
    UILabel *_totalMoneyTitle; //标的总额标识
    UILabel *_totalMoney; //标的总额
    UIImageView *_awardImageV; //奖励
    UILabel *_awardLabel; //奖励
}

@end

@implementation YPRecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        CGFloat fontSize = (iPhone4 || iPhone5)?13:15;
        _titleLabel = [UILabel creatWithFrame:CGRectMake(autoScaleW(15), 10, fontSize*13+10, 20)
                                         font:[UIFont systemFontOfSize:fontSize]
                                         text:@""
                                    textColor:[UIColor blackColor]
                                textAlignment:0];
        [self.contentView addSubview:_titleLabel];
        
        
        
        _borrowTypeView = [[YPBorrowTypeView alloc]initWithFrame:CGRectMake(_titleLabel.right + 5, _titleLabel.top , 0, _titleLabel.height)];
        [self.contentView addSubview:_borrowTypeView];
        
        
        
        UIImage *awardImage = [UIImage imageNamed:@"jiangli"];
        _awardImageV = [[UIImageView alloc]initWithImage:awardImage];
        _awardImageV.hidden = YES;
        [_awardImageV sizeToFit];
        _awardImageV.top = _borrowTypeView.top + 4;
        [self.contentView addSubview:_awardImageV];
        _awardLabel = [UILabel creatWithFrame:CGRectMake(17, 0, _awardImageV.width - 17, _awardImageV.height)
                                         font:[UIFont systemFontOfSize:10]
                                         text:@""
                                    textColor:[UIColor whiteColor]
                                textAlignment:0];
        [_awardImageV addSubview:_awardLabel];
        
        

        
        CGFloat labelWidth = (SCREEN_WIDTH - _titleLabel.left * 2)/3.f;
        _aprLabel = [UILabel creatWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + 20, labelWidth, 20)
                                       font:[UIFont systemFontOfSize:12]
                                       text:@""
                                  textColor:kColor_AppNavColor
                              textAlignment:0];
        [self.contentView addSubview:_aprLabel];
        UILabel *apr = [UILabel creatWithFrame:CGRectMake(_aprLabel.left, _aprLabel.bottom + 5, labelWidth, 20)
                                          font:[UIFont systemFontOfSize:12]
                                          text:@"参考年化收益率"
                                     textColor:kColor_greyAshTitleColor
                                 textAlignment:0];
        [self.contentView addSubview:apr];
        
        
        
        
        _timeLimitLabel = [UILabel creatWithFrame:CGRectMake(_aprLabel.right, _aprLabel.top, labelWidth, _aprLabel.height)
                                         font:[UIFont systemFontOfSize:15]
                                         text:@""
                                    textColor:[UIColor blackColor]
                                textAlignment:1];
        [self.contentView addSubview:_timeLimitLabel];
        _timeLimit = [UILabel creatWithFrame:CGRectMake(_timeLimitLabel.left, apr.top, labelWidth, apr.height)
                                          font:apr.font
                                          text:@"项目期限"
                                     textColor:kColor_greyAshTitleColor
                                 textAlignment:1];
        [self.contentView addSubview:_timeLimit];
        
        
        
        
        _totalMoney = [UILabel creatWithFrame:CGRectMake(_timeLimitLabel.right, _aprLabel.top, labelWidth, _aprLabel.height)
                                         font:[UIFont systemFontOfSize:15]
                                         text:@""
                                    textColor:[UIColor blackColor]
                                textAlignment:1];
        [self.contentView addSubview:_totalMoney];
        _totalMoneyTitle = [UILabel creatWithFrame:CGRectMake(_totalMoney.left, apr.top, labelWidth, apr.height)
                                                font:apr.font
                                                text:@"标的总额"
                                           textColor:kColor_greyAshTitleColor
                                       textAlignment:1];
        [self.contentView addSubview:_totalMoneyTitle];
        
        
        
        [self.contentView addLineWithFrame:CGRectMake(_aprLabel.right, _aprLabel.top, 0.5, 45) color:colorWithRGB(230, 230, 230)];
        [self.contentView addLineWithFrame:CGRectMake(_timeLimitLabel.right, _timeLimitLabel.top, 0.5, 45) color:colorWithRGB(230, 230, 230)];
        
    }
    
    return self;
}

- (void)setModel:(RecommendModel *)model{
    
    _model = model;
    
    _titleLabel.text = model.title;
    CGFloat titleW = [_titleLabel.text widthWithFont:_titleLabel.font height:_titleLabel.height];
    if (titleW <= _titleLabel.width) {
        _borrowTypeView.left = _titleLabel.left + titleW + 5;
    }else{
        _borrowTypeView.left = _titleLabel.right + 5;
    }
    _timeLimitLabel.text = [NSString stringWithFormat:@"%@个月",_model.time_limit];
    _aprLabel.attributedText = [AppTools getAttributedStringWithRate:model.apr rateAdd:model.apr_add fontSize:20];
    _totalMoney.text = _model.account;
    [_borrowTypeView setBorrowType:model.borrow_type];
    if (model.award.floatValue > 0) {
        _awardImageV.hidden = NO;
        _awardImageV.left = _borrowTypeView.right - 2;
        _awardLabel.text = [NSString stringWithFormat:@"%@%%",model.award];
    }else{
        _awardImageV.hidden = YES;
    }
    
}

- (void)setZqModel:(ZQListModel *)zqModel{
    
    _zqModel = zqModel;
    
    
    _titleLabel.text = _zqModel.title;
    _timeLimit.text = @"剩余期限";
    _timeLimitLabel.text =  [NSString stringWithFormat:@"%@期",_zqModel.remain_periods];
    _aprLabel.attributedText = [AppTools getAttributedStringWithRate:_zqModel.apr rateAdd:nil fontSize:20];
    _totalMoney.text = _zqModel.status.integerValue == 0?_zqModel.sale_price:@"";
    
    if (_zqModel.status.integerValue == 1) {
        _totalMoneyTitle.text = @"已售罄";
        _totalMoneyTitle.centerY = _aprLabel.bottom;
        _totalMoneyTitle.font = [UIFont systemFontOfSize:15];
    }else{
        _totalMoneyTitle.text = @"转让价格";
        _totalMoneyTitle.top = _aprLabel.bottom + 5;
        _totalMoneyTitle.font = [UIFont systemFontOfSize:12];
    }
    _titleLabel.textColor = _zqModel.status.integerValue == 0?[UIColor blackColor]:kColor_greyAshTitleColor;
    _aprLabel.textColor = _zqModel.status.integerValue == 0?kColor_AppNavColor:kColor_greyAshTitleColor;
    _timeLimitLabel.textColor = _zqModel.status.integerValue == 0?[UIColor blackColor]:kColor_greyAshTitleColor;
    _totalMoney.textColor = _zqModel.status.integerValue == 0?[UIColor blackColor]:kColor_greyAshTitleColor;
    
}


@end
